<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary" @click="addGoodsCate">添加分类</el-button>
        </el-col>
      </el-row>
      <!--
        data ：定义是我们的数据
        columns：定义列的信息
        selection-type 是否添加选择框
        expand-type  是否添加展开行
        show-index  是否显示索引
        index-text 定义索引的名称
        border 是否添加竖向的边框
      -->
      <table-tree
        class="table_tree"
        :data="goodsCateList"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        show-index
        border
        index-text="#"
      >
        <template slot="isTrue" slot-scope="scope">
          <i class="el-icon-success" v-if="scope.row.cat_deleted" style="color:lightgreen"></i>
          <i class="el-icon-error" v-else style="color:red"></i>
        </template>
        <template slot="sort" slot-scope="scope">
          <el-tag type="primary" v-if="scope.row.cat_level === 0" size="mini">一级</el-tag>
          <el-tag type="warning" v-else-if="scope.row.cat_level === 1" size="mini">二级</el-tag>
          <el-tag type="danger" v-else size="mini">三级</el-tag>
        </template>
        <template slot="operate" slot-scope="scope">
          <!-- 编辑用户按钮 -->
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="mini"
            @click="editGoodsCate(scope.row.cat_id)"
          >编辑</el-button>
          <!-- 删除用户按钮 -->
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            @click="delGoodsCate(scope.row.cat_id)"
          >删除</el-button>
        </template>
      </table-tree>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="cateInfo.pagenum"
        :page-sizes="[2, 5, 10, 20]"
        :page-size="cateInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <!-- 编辑 -->
    <el-dialog title="修改分类" :visible.sync="editDialogVisible" width="50%" @close="editDialogClose">
      <el-form :model="editInfo" :rules="editRules" label-width="80px" ref="editRef">
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="editInfo.cat_name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitGoodsCate">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 添加分类
      visible  控制对话框的显示和隐藏
    -->
    <el-dialog title="添加分类" :visible.sync="addDialogVisible" width="50%" @close="addDialogClose">
      <el-form label-width="80px" :model="addForm" :rules="addFormRules" ref="addFormRef">
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
          <!--
            options 定义我们的数据
           -->
          <el-cascader
            v-model="selectedvals"
            :options="goodsCasList"
            :props="addProps"
            filterable
            change-on-select
            expandTrigger='hover'
            clearable
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addDialogCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import * as goods from '../../api/goods'
export default {
  data () {
    return {
      // 商品列表数据
      goodsCateList: [],
      // 列的配置
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          /*
            label 定义列的名称
            prop  列的属性
            type  模板的类型
            template 插槽的名称
        */
          label: '是否有效',
          prop: 'cat_deleted',
          type: 'template',
          template: 'isTrue'
        },
        {
          label: '排序',
          prop: 'cat_level',
          type: 'template',
          template: 'sort'
        },
        {
          label: '操作',
          prop: '',
          type: 'template',
          template: 'operate'
        }
      ],
      cateInfo: {
        type: '3',
        pagenum: 1,
        pagesize: 5
      },
      total: 0,
      editDialogVisible: false,
      editInfo: {
        cat_name: ''
      },
      editRules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
        ]
      },
      addDialogVisible: false,
      addForm: {
        // fuId为0，说明是一级分类
        cat_pid: 0,
        cat_name: '',
        //  0 表示一级分类
        cat_level: 0
      },
      addFormRules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
        ]
      },
      selectedvals: [],
      goodsCasList: [],
      addProps: {
        // 显示的文本
        label: 'cat_name',
        // 选中之后要获取的id
        value: 'cat_id',
        children: 'children'

      }
    }
  },
  created () {
    this.getGoodsCateList()
  },
  methods: {
    async getGoodsCateList (cateInfo = this.cateInfo) {
      const { data: res } = await goods.getGoodsCateList(cateInfo)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.total = res.data.total
      this.goodsCateList = res.data.result
      console.log(res)
    },
    handleSizeChange (newSize) {
      this.cateInfo.pagesize = newSize
      this.getGoodsCateList()
    },
    handleCurrentChange (newPage) {
      this.cateInfo.pagenum = newPage
      this.getGoodsCateList()
    },
    async editGoodsCate (id) {
      const { data: res } = await goods.getGoodsCateById(id)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.editInfo = res.data
      this.editDialogVisible = true
    },
    submitGoodsCate () {
      this.$refs.editRef.validate(async validate => {
        if (!validate) return
        const { data: res } = await goods.submitGoodsCate(
          this.editInfo.cat_id,
          this.editInfo
        )
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
        this.$message.success(res.meta.msg)
        this.getGoodsCateList()
        this.editDialogVisible = false
      })
    },
    editDialogClose () {
      this.$refs.editRef.resetFields()
    },
    delGoodsCate (id) {
      this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const { data: res } = await goods.delGoodsCate(id)
          if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
          this.$message.success(res.meta.msg)
          this.getGoodsCateList()
        })
        .catch(() => {
          this.$message.info('已取消删除')
        })
    },
    async addGoodsCate () {
      const { data: res } = await goods.getGoodsCateList({ type: 2 })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.goodsCasList = res.data
      this.addDialogVisible = true
      console.log(this.selectedvals)
    },
    addDialogClose () {
      this.$refs.addFormRef.resetFields()
      this.selectedvals = []
      this.addForm.cat_pid = 0
      this.addForm.cat_level = 0
    },
    async addDialogCate () {
      // 分类的父Id是selectedvals的最后一项
      // 分类等级是selectedvals的长度
      this.selectedvals.length > 0 ? this.addForm.cat_pid = this.selectedvals[this.selectedvals.length - 1] : this.addForm.cat_pid = 0
      this.addForm.cat_level = this.selectedvals.length
      const { data: res } = await goods.postGoodsCate(this.addForm)
      if (res.meta.status !== 201) return this.$message.error(res.meta.msg)
      this.$message.success(res.meta.msg)
      this.getGoodsCateList()
      console.log(res)
      this.addDialogVisible = false
    }
  }
}
</script>

<style lang="less" scoped>
.table_tree {
  margin: 15px 0;
}
.el-cascader{
  width: 100%;
}
</style>
